Obafemi Emmanuel

File Handling in Python: Reading and Writing Files

Published 3 months ago

Python provides built-in functions for reading and writing files. The most common method for working with files is using the open() function, which allows you to work with files in different modes like reading ('r'), writing ('w'), and appending ('a').


Reading Files

To read a file, you use the open() function followed by the read() method. Here’s an example:

python
CopyEdit
# Opening a file in read mode
with open('example.txt', 'r') as file:
    content = file.read()
    print(content)

In the example above, we use the with open() statement, which automatically closes the file after the operations are complete, ensuring that resources are freed.

You can also read a file line by line:

python
CopyEdit
with open('example.txt', 'r') as file:
    for line in file:
        print(line.strip())  # strip() removes extra whitespace/newlines

Writing to Files

When you want to write to a file, you can open the file in write mode ('w') or append mode ('a'):

python
CopyEdit
# Writing to a file (overwrites the file)
with open('example.txt', 'w') as file:
    file.write("Hello, Python!")

To append data to an existing file without overwriting it:

python
CopyEdit
# Appending to a file
with open('example.txt', 'a') as file:
    file.write("\nAdding more content!")

The write() method will not add a newline unless explicitly specified. If you want to add new lines, make sure to include \n.


2. Working with JSON and CSV in Python

Python makes it easy to work with JSON (JavaScript Object Notation) and CSV (Comma Separated Values) files. These formats are commonly used for storing structured data. Let’s look at how to handle each.

Working with JSON Files

JSON is a lightweight data-interchange format, often used for APIs and configuration files. Python provides the json module to easily parse JSON data into Python objects and vice versa.


Reading JSON Data
python
CopyEdit
import json

# Reading JSON from a file
with open('data.json', 'r') as file:
    data = json.load(file)  # Load JSON data into a Python dictionary
    print(data)
Writing JSON Data

To write data to a JSON file:

python
CopyEdit
import json

# Example data to write to a JSON file
data = {'name': 'John', 'age': 30, 'city': 'New York'}

# Writing data to a JSON file
with open('data.json', 'w') as file:
    json.dump(data, file, indent=4)  # The indent argument makes the JSON readable

Working with CSV Files

CSV files are widely used to store tabular data, and Python has a built-in csv module for reading and writing CSV data.


Reading CSV Files
python
CopyEdit
import csv

# Reading data from a CSV file
with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    for row in reader:
        print(row)

If your CSV file contains headers, you can use csv.DictReader to read the rows as dictionaries:

python
CopyEdit
with open('data.csv', 'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        print(row['name'], row['age'])
Writing to CSV Files

To write data to a CSV file:

python
CopyEdit
import csv

# Data to write into CSV
data = [['name', 'age', 'city'],
        ['John', 30, 'New York'],
        ['Jane', 25, 'Los Angeles']]

with open('output.csv', 'w', newline='') as file:
    writer = csv.writer(file)
    writer.writerows(data)

For writing dictionaries to a CSV file, you can use csv.DictWriter:

python
CopyEdit
import csv

# Data to write into CSV
data = [{'name': 'John', 'age': 30, 'city': 'New York'},
        {'name': 'Jane', 'age': 25, 'city': 'Los Angeles'}]

with open('output.csv', 'w', newline='') as file:
    fieldnames = ['name', 'age', 'city']
    writer = csv.DictWriter(file, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(data)

3. Database Connectivity with SQLite and MySQL

Python also excels in interacting with databases. The two most commonly used databases are SQLite and MySQL. Let's look at how to work with them.

SQLite Database Connectivity

SQLite is a lightweight database that comes bundled with Python, making it easy to use for smaller applications.


Connecting to SQLite
python
CopyEdit
import sqlite3

# Connecting to SQLite database (it creates a file if it doesn't exist)
connection = sqlite3.connect('example.db')
cursor = connection.cursor()

# Creating a table
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)''')

# Inserting data into the table
cursor.execute('''INSERT INTO users (name, age) VALUES ('John', 30)''')

# Committing changes and closing the connection
connection.commit()
connection.close()
Fetching Data from SQLite
python
CopyEdit
import sqlite3

# Connecting to SQLite database
connection = sqlite3.connect('example.db')
cursor = connection.cursor()

# Fetching data
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
    print(row)

connection.close()

MySQL Database Connectivity

To work with MySQL, you need to install the mysql-connector library:

bash
CopyEdit
pip install mysql-connector-python
Connecting to MySQL
python
CopyEdit
import mysql.connector

# Connecting to MySQL
connection = mysql.connector.connect(
    host='localhost',
    user='yourusername',
    password='yourpassword',
    database='yourdatabase'
)

cursor = connection.cursor()

# Creating a table
cursor.execute('''CREATE TABLE IF NOT EXISTS users (id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(255), age INT)''')

# Inserting data
cursor.execute('''INSERT INTO users (name, age) VALUES ('John', 30)''')

# Committing and closing the connection
connection.commit()
connection.close()
Fetching Data from MySQL
python
CopyEdit
import mysql.connector

# Connecting to MySQL
connection = mysql.connector.connect(
    host='localhost',
    user='yourusername',
    password='yourpassword',
    database='yourdatabase'
)

cursor = connection.cursor()

# Fetching data
cursor.execute('SELECT * FROM users')
rows = cursor.fetchall()
for row in rows:
    print(row)

connection.close()

Conclusion

Handling data in Python is a powerful and straightforward process thanks to its rich set of built-in libraries and tools. Whether you're reading and writing files, working with structured data formats like JSON and CSV, or connecting to databases like SQLite and MySQL, Python makes it all possible with minimal effort. By mastering these techniques, you’ll be able to efficiently manage and process data for a variety of applications, from simple scripts to complex systems.


Leave a Comment


Choose Colour